package com.aniu.smslisten.receiver;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.aniu.smslisten.service.SmsListenService;
import com.aniu.smslisten.util.LogToFile;

public class ServiceAlarmWorker extends Worker {
    public ServiceAlarmWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        LogToFile.log(getApplicationContext(), this.getClass().getSimpleName(), "ServiceAlarmWorker doWork called");

        // 判断SmsListenService是否正在运行
        if (!SmsListenService.isServiceRunning(getApplicationContext())) {
            Intent serviceIntent = new Intent(getApplicationContext(), SmsListenService.class);
            getApplicationContext().startForegroundService(serviceIntent);
        } else {
            // 正常运行时调用SmsListenService的setAlarm()设置下一个时间点的检查alarm
//            SmsListenService.scheduleWorkManagerAlarm(getApplicationContext());
        }

        return Result.success();
    }
}